Module modWRDBProject

    ' ''' <summary>
    ' ''' Path to where WRDB dll is located
    ' ''' </summary>
    'Friend DLLPath As String

    ''' <summary>
    ''' REGAPP is registry key; note that the registry is ONLY used to store form positions and sizes and the path to WRDB.ini; all other program information is stored in INI files for transportability
    ''' </summary>
    Friend Const REGAPP = "WRDB6"

    ''' <summary>
    ''' Utility function to easily add a column to a data table
    ''' </summary>
    ''' <param name="ColumnName">Name of field</param>
    ''' <param name="Caption">Caption for grid heading</param>
    ''' <param name="DataType">Data type</param>
    ''' <param name="MaxLength">Maximum field length (for strings)</param>
    ''' <param name="DefaultValue">Default value to assign to field</param>
    ''' <returns>Data table column</returns>
    Friend Function NewColumn(ByVal ColumnName As String, ByVal Caption As String, ByVal DataType As System.Type, Optional ByVal MaxLength As Integer = 0, Optional ByVal DefaultValue As Object = Nothing) As DataColumn
        Dim col As New DataColumn()
        With col
            .ColumnName = ColumnName
            .Caption = Caption
            .DataType = DataType
            If DefaultValue Is Nothing Then
                .DefaultValue = DBNull.Value
            Else
                .DefaultValue = DefaultValue
            End If
            If DataType Is GetType(String) Then .MaxLength = MaxLength
        End With
        Return col
    End Function


#Region "Encryption/Decryption Routines"

    ' Encrypt the text
    Friend Function EncryptText(ByVal strText As String) As String
        Return Encrypt(strText, "&%#@?,:*")
    End Function

    'Decrypt the text 
    <DebuggerStepThrough()>
    Friend Function DecryptText(ByVal strText As String) As String
        Try
            Return Decrypt(strText, "&%#@?,:*")
        Catch ex As Exception
            Return ""
        End Try
    End Function

    'The function used to encrypt the text
    Private Function Encrypt(ByVal strText As String, ByVal strEncrKey As String) As String
        If strText = "" Then Return ""
        Dim byKey() As Byte = {}
        Dim IV() As Byte = {&H12, &H34, &H56, &H78, &H90, &HAB, &HCD, &HEF}

        byKey = System.Text.Encoding.UTF8.GetBytes(Left(strEncrKey, 8))

        Dim des As New System.Security.Cryptography.DESCryptoServiceProvider()
        Dim inputByteArray() As Byte = System.Text.Encoding.UTF8.GetBytes(strText)
        Dim ms As New IO.MemoryStream()
        Dim cs As New Security.Cryptography.CryptoStream(ms, des.CreateEncryptor(byKey, IV), Security.Cryptography.CryptoStreamMode.Write)
        cs.Write(inputByteArray, 0, inputByteArray.Length)
        cs.FlushFinalBlock()
        Return Convert.ToBase64String(ms.ToArray())
    End Function

    'The function used to decrypt the text
    <DebuggerStepThrough()>
    Private Function Decrypt(ByVal strText As String, ByVal sDecrKey As String) As String
        If strText = "" Then Return ""
        Dim byKey() As Byte = {}
        Dim IV() As Byte = {&H12, &H34, &H56, &H78, &H90, &HAB, &HCD, &HEF}
        Dim inputByteArray(strText.Length) As Byte

        Try
            byKey = System.Text.Encoding.UTF8.GetBytes(Left(sDecrKey, 8))
            Dim des As New Security.Cryptography.DESCryptoServiceProvider()
            inputByteArray = Convert.FromBase64String(strText)
            Dim ms As New IO.MemoryStream()
            Dim cs As New Security.Cryptography.CryptoStream(ms, des.CreateDecryptor(byKey, IV), Security.Cryptography.CryptoStreamMode.Write)

            cs.Write(inputByteArray, 0, inputByteArray.Length)
            cs.FlushFinalBlock()
            Dim encoding As System.Text.Encoding = System.Text.Encoding.UTF8

            Return encoding.GetString(ms.ToArray())
        Catch ex As Exception
            Throw New ApplicationException("Unable to decrypt password; please respecify.")
            Return ""
        End Try
    End Function

#End Region

    Friend Function ArrayToCSV(ByVal ar() As String) As String
        Dim csv As String = ""
        For Each s As String In ar
            csv &= IIf(csv = "", "", ",") & s.Trim
        Next
        Return csv
    End Function

    ''' <summary>
    ''' Test object; if dbnull, nothing, empty string, or minimum value, return default value
    ''' </summary>
    Friend Function TestNull(ByVal Value As Object, ByVal DefaultValue As Object) As Object
        If IsDBNull(Value) OrElse
            IsNothing(Value) OrElse
            (TypeOf Value Is String AndAlso (String.IsNullOrEmpty(Value) OrElse Value.ToString.ToUpper = "<NULL>")) OrElse
            (TypeOf Value Is Date AndAlso Value = DateTime.MinValue) OrElse
            (IsNumeric(Value) AndAlso Value = Single.MinValue) Then Return DefaultValue Else Return Value
    End Function

    ''' <summary>
    ''' Test object; if dbnull, nothing or empty string, return default value
    ''' </summary>
    Friend Sub SetIfNull(ByRef Value As Object, ByVal DefaultValue As Object)
        If IsDBNull(Value) OrElse IsNothing(Value) OrElse (TypeOf Value Is String AndAlso Value = "") Then Value = DefaultValue
    End Sub

End Module
